package com.example.certificate.execption;

import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.UnexpectedTypeException;

/**
 * @Description: 包含统一异常处理的异常捕捉器
 */

@Slf4j
@RestControllerAdvice
public class ExceptionUtil {


    // 捕捉HttpRequestMethodNotSupportedException
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultEntity handleHttpMethodException(Exception e) {
        return commonResult(ResultConstant.HTTP_METHOD_NOT_SUPPORT, e);
    }

    // 捕捉UnauthorizedException
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public ResultEntity handle401(Exception e) {
        return commonResult(ResultConstant.ACCESS_DENIED, e);
    }

    //捕捉参数缺失异常MissingServletRequestParameterException
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultEntity handleMethodParamMissingException(Exception e) {
        return commonResult(ResultConstant.PARAM_ERROR, e);
    }

    //捕捉参数校验异常MethodArgumentNotValidException
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({UnexpectedTypeException.class, MethodArgumentNotValidException.class})
    public ResultEntity handleMethodParamNotValidException(MethodArgumentNotValidException e) {
        //获取 bean字段上的message错误  springmvc有默认的
        if (e.getBindingResult().hasErrors()) {
            String message = e.getBindingResult().getFieldError().getDefaultMessage();
            if (!"".equals(message)) {
                return initResult(ResultConstant.PARAM_ERROR.getResultCode(), message);
            }
        }
        return commonResult(ResultConstant.PARAM_ERROR, e);
    }

    //捕捉业务异常(操作异常)
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BusinessException.class)
    public ResultEntity handleBusinessException(BusinessException e) {
        return initResult(e.getErrorCode(), e.getErrorDesc());
    }

    // 捕捉其他所有异常
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultEntity globalException(Exception e) {
        return commonResult(ResultConstant.INTERNAL_SERVER_ERROR, e);
    }


     /**
     * Description: 常用错误 带异常信息
     */

    private ResultEntity commonResult(ResultConstant constant, Exception e) {
        //打印异常日志,用于排查
        log.info(e.getMessage(), e);
        return getResult(constant);
    }

     /**
     * Description: 常用错误 不带异常信息
     */

    public ResultEntity commonResult(ResultConstant constant) {
        return getResult(constant);
    }


     /**
     * Description: 国际化 统一错误码
     */

    public ResultEntity initResult(String code, String message) {
        //打印异常日志,用于排查
        log.info("其他所有错误" + message);
//        String mes = localeMessage.getMessage(message);
        if ("".equals(message)) {
            return new ResultEntity(message);
        }
        return new ResultEntity(code, message);
    }
    public void initResult(ResultEntity result, String message) {
        result.setResultCode(ResultConstant.OPERATE_FAIL.getResultCode());
        result.setDesc(message);
    }


    private ResultEntity getResult(ResultConstant constant) {
        ResultEntity result = new ResultEntity();
        result.setResultCode(constant.getResultCode());
        result.setDesc(constant.getMessage());
        return result;
    }
}
